<?php
  // Rebuild .info data.
  system_rebuild_theme_data();
  // Rebuild theme registry.
  drupal_theme_rebuild();

function events_theme() {
  $hooks = array();

  $hooks['registration_form'] = array(
    'render element' => 'form',
  );

  return $hooks;
}

function events_preprocess_node(&$variables) {

  if ($variables['type'] == 'event') {
    $settings = registration_entity_settings('node', $variables['nid']);
    $status = $settings['status'];
    $open = isset($settings['open']) ? strtotime($settings['open']) : NULL;
    $close = isset($settings['close']) ? strtotime($settings['close']) : NULL;
    $now = REQUEST_TIME;

    // if ($now > $close) {
      $variables['event_status']['class'] = 'closed';
      $variables['event_status']['label'] = '已结束';
    // }
    if ($status) {
      if ($close && $now > $close) {
        $variables['event_status']['class'] = 'closed';
        $variables['event_status']['label'] = '已结束';
      }
      elseif ($open && $now < $open) {
        $variables['event_status']['class'] = 'upcoming';
        $variables['event_status']['label'] = '准备中';
      }
      else {
        $variables['event_status']['class'] = 'opening';
        $variables['event_status']['label'] = '进行中';
      }     
    }

  }
}

function events_form_registration_form_alter(&$form, &$form_state, $form_id) {
  foreach (element_children($form) as $key) {
    if($form[$key]['#type'] == 'textfield') {
      $form[$key]['#pre_render'][] = '_events_form_element_textfield_process_placeholder';
    }
    $form[$key]['#title_display'] = 'invisible';
  }

  $form['anon_mail']['#description'] = '输入有效的Email, 用于接收活动报名结果及相关信息';

  $form['actions']['submit']['#value'] = '报名申请';
  $form['actions']['submit']['#attributes']['class'][] = 'am-btn-success';
  $form['actions']['cancel']['#access'] = false;
}

function events_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['instance']['entity_type'] == 'registration') {
    if ($element['value']['#type'] == 'textfield') {
      $element['value']['#pre_render'][] = '_events_form_element_textfield_process_placeholder';
      $element['value']['#title_display'] = 'invisible';
    }
  }
}

function _events_form_element_textfield_process_placeholder($element) {
  $element['#attributes']['placeholder'] = !empty($element['#description']) ? $element['#description'] : $element['#title'] ;
  unset($element['#description']);
  return $element;
}


?>